package com.example.myapplication;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener{
    //好处是初始化时就创建出来了,
    public TextView viewById;
    public Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局
        setContentView(R.layout.layout2);
        //根据ID查找TextView对象
        viewById = findViewById(R.id.time);
        //设置监听
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button1).setOnClickListener(this);

        findViewById(R.id.disable).setOnClickListener(this);
        findViewById(R.id.enabled).setOnClickListener(this);
        button = findViewById(R.id.button4);

        findViewById(R.id.button1).setOnLongClickListener(v ->{
                    int id = v.getId();
                    if(id == R.id.button1){
                        //如果条件相等的情况下执行方法,,,,,
                    }
                    String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                    this.viewById.setText(format+" 长按点击 "+id+" "+((TextView)v).getText());
                    //是否向上传递长按事件
                    return true;
                }
        );
        //触摸事件
        findViewById(R.id.button2).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                MainActivity2.this.viewById.setText(format+" 双击事件 "+" "+((TextView)v).getText());
                return false;
            }
        });
    }

    //点击公共方法,等效上方setOnTouchListener等方法的匿名内部类
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.disable:
                button.setEnabled(false);
                break;
            case R.id.enabled:
                button.setEnabled(true);
                break;
        }
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        viewById.setText(format+" 点击事件 "+id+" "+((TextView)v).getText());
    }
}
